package com.gitee.ywj1352.jpa.jpa.service;

import com.gitee.ywj1352.jpa.jpa.dao.UserDAO;
import com.gitee.ywj1352.jpa.jpa.entity.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.ConnectionHandle;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.security.PermitAll;
import java.sql.Connection;
import java.util.List;

@Service("userService")
public class UserServiceImpl {
    @Autowired
    private BeanFactory factory;

    @Autowired
    private UserDAO userDAO;

    public List<User> getUserList() {
        JpaTransactionManager txManager =  (JpaTransactionManager)factory.getBean(PlatformTransactionManager.class);
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = txManager.getTransaction(def);
        User user = new User();
        user.setName("rollback");user.setAddress("nanjing");
        userDAO.save(user);
        userDAO.flush();
        txManager.rollback(status);
        JpaDialect jpaDialect = txManager.getJpaDialect();
//        ConnectionHandle jdbcConnection = jpaDialect.getJdbcConnection();
//        Connection connection = jdbcConnection.getConnection();
        return userDAO.findAll();
    }


//    public void testdelivery() {
//        //定义事务隔离级别，传播行为，
//        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
//        def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
//        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//        JpaTransactionManager txManager = (JpaTransactionManager) factory.getBean(PlatformTransactionManager.class);
//        //事务状态类，通过PlatformTransactionManager的getTransaction方法根据事务定义获取；获取事务状态后，Spring根据传播行为来决定如何开启事务
//        TransactionStatus status = txManager.getTransaction(def);
//        DataSource dataSource = factory.getBean(DataSource.class);
//        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//        int i = jdbcTemplate.queryForInt(COUNT_SQL);
//        System.out.println("表中记录总数：" + i);
//        try {
//            jdbcTemplate.update(INSERT_SQL, "1");
//            txManager.commit(status);  //提交status中绑定的事务
//        } catch (RuntimeException e) {
//            txManager.rollback(status);  //回滚
//        }
//        i = jdbcTemplate.queryForInt(COUNT_SQL);
//        System.out.println("表中记录总数：" + i);
//    }


    @PermitAll
    public String hi() {
        return "hi";
    }


}
